package com.alibaba.nacos;

import com.alibaba.nacos.console.NacosConsole;
import com.alibaba.nacos.core.listener.startup.NacosStartUp;
import com.alibaba.nacos.core.listener.startup.NacosStartUpManager;
import com.alibaba.nacos.sys.env.Constants;
import com.alibaba.nacos.sys.env.DeploymentType;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

public class SoloNacosApplication {

    public static void main(String[] args) {
        // 设置单机模式
        System.setProperty("nacos.standalone", "true");

        // 设置部署类型（merged/server/console）
        String type= System.getProperty(Constants.NACOS_DEPLOYMENT_TYPE,
                Constants.NACOS_DEPLOYMENT_TYPE_MERGED);
        DeploymentType deploymentType= DeploymentType.getType(type);
        EnvUtil.setDeploymentType(deploymentType);

        // 多阶段启动：核心服务 → Web 服务 → 控制台
        // 第一阶段：启动核心服务
        NacosStartUpManager.start(NacosStartUp.CORE_START_UP_PHASE);
        ConfigurableApplicationContext coreContext=
                new SpringApplicationBuilder(NacosServerBasicApplication.class)
                        .web(WebApplicationType.NONE)
                        .run(args);

        // 第二阶段：启动服务端 Web
        NacosStartUpManager.start(NacosStartUp.WEB_START_UP_PHASE);
        ConfigurableApplicationContext serverWebContext=
                new SpringApplicationBuilder(NacosServerWebApplication.class)
                        .parent(coreContext)
                        .run(args);

        // 第三阶段：启动控制台
        NacosStartUpManager.start(NacosStartUp.CONSOLE_START_UP_PHASE);
        ConfigurableApplicationContext consoleContext=
                new SpringApplicationBuilder(NacosConsole.class)
                        .parent(coreContext)
                        .run(args);
    }
}